package cn.iocoder.yudao.module.dr.service.collectionattachment;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.dr.controller.admin.collectionattachment.vo.*;
import cn.iocoder.yudao.module.dr.dal.dataobject.collectionattachment.CollectionAttachmentDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.dr.dal.mysql.collectionattachment.CollectionAttachmentMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.dr.enums.ErrorCodeConstants.*;

/**
 * 藏品附件 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class CollectionAttachmentServiceImpl implements CollectionAttachmentService {

    @Resource
    private CollectionAttachmentMapper collectionAttachmentMapper;

    @Override
    public Long createCollectionAttachment(CollectionAttachmentSaveReqVO createReqVO) {
        // 插入
        CollectionAttachmentDO collectionAttachment = BeanUtils.toBean(createReqVO, CollectionAttachmentDO.class);
        collectionAttachmentMapper.insert(collectionAttachment);
        // 返回
        return collectionAttachment.getId();
    }

    @Override
    public void updateCollectionAttachment(CollectionAttachmentSaveReqVO updateReqVO) {
        // 校验存在
        validateCollectionAttachmentExists(updateReqVO.getId());
        // 更新
        CollectionAttachmentDO updateObj = BeanUtils.toBean(updateReqVO, CollectionAttachmentDO.class);
        collectionAttachmentMapper.updateById(updateObj);
    }

    @Override
    public void deleteCollectionAttachment(Long id) {
        // 校验存在
        validateCollectionAttachmentExists(id);
        // 删除
        collectionAttachmentMapper.deleteById(id);
    }

    private void validateCollectionAttachmentExists(Long id) {
        if (collectionAttachmentMapper.selectById(id) == null) {
            throw exception(COLLECTION_ATTACHMENT_NOT_EXISTS);
        }
    }

    @Override
    public CollectionAttachmentDO getCollectionAttachment(Long id) {
        return collectionAttachmentMapper.selectById(id);
    }

    @Override
    public PageResult<CollectionAttachmentDO> getCollectionAttachmentPage(CollectionAttachmentPageReqVO pageReqVO) {
        return collectionAttachmentMapper.getCollectionAttachmentPage(pageReqVO);
    }

}